<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page="page"
               @row-del="rowDel"
               v-model="form"
               :permission="permissionList"
               @row-update="rowUpdate"
               @row-save="rowSave"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.notice_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="{row}"
                      slot="category">
              <el-tag>{{row.categoryName}}</el-tag>
            </template>
          </avue-crud>
        </basic-container>
      </template>
<script>
  import {getList, remove, update, add, getNotice} from "@/api/system/doctor";
  import {mapGetters} from "vuex";
  export default {
    data() {
      return {
        form: {},
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
             {
              label: "姓名",
              prop: "name",
              row: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
           {
              label: "性别",
              prop: "sex",
              type:"radio",
              row: true,
              dicData: [
                {
                  label: "男",
                  value: 1
                },
                {
                  label: "女",
                  value: 2
                }
              ],
              rules: [{
                required: true,
                message: "请选择性别 ",
                trigger: "blur"
              }]
            }, 
            {
              label: "手机号",
              prop: "phone",
              row: true,
              search: false,
              rules: [{
                required: true,
                message: "请输入手机号 ",
                trigger: "blur",
              },
              {
                pattern: /^1[34578]\d{9}$/, 
                message: '请输入11位数字的正确手机号', 
                trigger: 'blur'
              }
              ]
            },
             {
              label: "邮箱",
              prop: "email",
              row: true,
              rules: [{
                required: true,
                message: "请输入邮箱 ",
                trigger: "blur",
              },
               {
                pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, 
                message: '请输入正确的邮箱地址', 
                trigger: 'blur'
              }]
            },
            {
              label: "类型",
              type: "select",
              row: true,
              dicUrl:"/api/doctorCategory/selectAllDoctorCategory",
              dicMethod:"post",
              props: {
                label: "type",
                value: "id"
              },
              prop: "doctorCategoryid",
              search: false,   
              rules: [{
                required: true,
                message: "请选择类型",
                trigger: "blur"
              }]
            },
            {
              label: "生日",
              prop: "birthday",
              type: "date",
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd hh:mm:ss",
              row: true,
              rules: [{
                required: true,
                message: "请选择您的生日",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.doctor_add, false ),
          viewBtn: this.vaildData(this.permission.doctor_view, false),
          delBtn: this.vaildData(this.permission.doctor_delete, false),
          editBtn: this.vaildData(this.permission.doctor_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, loading, done) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
        });
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);

      },
      searchChange(params) {
        this.query = params;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getNotice(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          var data = res.data.data;
          this.page.total = data.totalSize;
          this.data = data.pageList;
        });
      }
    }
  };
</script>

<style>
</style>
